﻿Module modParser
    Class Parser
        Public ukuranWindowW As Single
        Public ukuranWindowH As Single
        Public Function P_Title(ByVal RTB As TransparentRichTextBox, ByVal input As String) As TransparentRichTextBox
            Dim PosNow As Integer
            If (String.IsNullOrEmpty(input) <> True) Then
                RTB.Clear()
                RTB.Text = input.Replace("<br>", vbCrLf)

                'title
                With RTB
                    Dim flag As Integer
                    Dim strt = 0
                    Dim ends = 0
                    Dim entr = 0

                    Dim q1 = "<title>"
                    Dim q2 = "</title>"
                    .SelectAll()
                    .SelectionFont = New Drawing.Font(.SelectionFont.FontFamily, (ukuranWindowH + ukuranWindowW) / 100)
                    ' Credit.SelectAll()
                    .SelectionAlignment = HorizontalAlignment.Center
                    'Credit.DeselectAll()
                    .DeselectAll()
                    PosNow = 0
                    For Each line As String In RTB.Lines
                        flag = 0
                        strt = 0
                        ends = 0

                        If (line.IndexOf(q1.ToCharArray()) > -1) Then
                            strt = line.IndexOf(q1.ToCharArray()) + 7
                        End If

                        If (line.IndexOf(q2.ToCharArray()) > -1) Then
                            ends = line.IndexOf(q2.ToCharArray())
                        End If
                        
                        While (ends < line.Length)
                            If (strt < ends) Then

                                flag += 1
                                Dim selectionst = PosNow + strt
                                Dim len = ends - strt
                                .Select()
                                .SelectionStart = selectionst
                                .SelectionLength = len

                                Dim currentFont As System.Drawing.Font = .SelectionFont
                                Dim newFontStyle As System.Drawing.FontStyle

                                newFontStyle = currentFont.Style + Drawing.FontStyle.Bold
                                .SelectionFont = New Drawing.Font(currentFont.FontFamily, currentFont.Size, newFontStyle)

                                'delete </title>
                                .SelectionStart = PosNow + ends
                                .SelectionLength = 8

                                Debug.Print(.SelectedText)
                                .SelectedText = ""

                                'delete <title>
                                .Select()
                                .SelectionStart = selectionst - 7
                                .SelectionLength = 7

                                'Debug.Print(.SelectedText)
                                .SelectedText = ""
                            End If

                            If line.IndexOf(q1.ToCharArray(), ends) = -1 Then
                                Exit While
                            End If

                            If line.IndexOf(q2.ToCharArray(), strt) = -1 Then
                                Exit While
                            End If
                            strt = line.IndexOf(q1.ToCharArray(), ends) + 7
                            ends = line.IndexOf(q2.ToCharArray(), strt)
                        End While

                        PosNow = line.Length + PosNow + 1 - ((7 + 8) * flag)
                    Next
                End With
            End If

            Return RTB
        End Function

        'section
        Public Function parsingText(ByRef secText As List(Of TransparentRichTextBox), ByVal input As String) As Boolean
            Dim RTB As New TransparentRichTextBox
            Dim temp As New ArrayList
            Dim PosNow As Integer
            Dim PosStart As Integer
            Dim sukses As Boolean = False

            If (String.IsNullOrEmpty(input) <> True) Then
                secText = New List(Of TransparentRichTextBox)
                'section
                With RTB
                    RTB.Text = input
                    Dim strt = 0
                    Dim ends = -1
                    Dim q1 = "<section>"
                    Dim q2 = "</section>"

                    PosNow = 0
                    PosStart = 0
                    If (.Text.IndexOf(q1) > -1 And .Text.IndexOf(q2) > -1) Then
                        While (ends < .Text.Length)
                            'MessageBox.Show(.Text)
                            If (.Text.IndexOf(q1.ToCharArray(), PosStart) > -1) Then
                                strt = .Text.IndexOf(q1.ToCharArray(), PosStart) + 9

                            ElseIf (.Text.IndexOf(q1.ToCharArray(), PosStart) = -1) Then
                                Dim newRTB As New TransparentRichTextBox
                                newRTB.Text = .Text
                                secText.Add(newRTB)

                                Exit While
                            End If

                            If (.Text.IndexOf(q2.ToCharArray(), strt) > -1) Then
                                ends = .Text.IndexOf(q2.ToCharArray(), strt)
                                PosStart = ends + 10
                                'Debug.Print("akhir: " + ends.ToString)
                            End If
                            If ends > -1 Then
                                .Select()
                                .SelectionStart = strt
                                .SelectionLength = ends - strt
                                Dim newRTB As New TransparentRichTextBox
                                newRTB.Text = .SelectedText
                                'P_Title2(newRTB)
                                secText.Add(newRTB)

                                PosNow = PosStart
                                strt = 0
                                ends = ends + 10
                            End If

                            PosNow = .Text.Length + PosNow + 1
                            'Debug.Print("PosNow: " + PosNow.ToString)
                        End While
                        sukses = True
                    Else
                        Dim newRTB As New TransparentRichTextBox
                        newRTB.Text = .Text
                        secText.Add(newRTB)

                    End If

                End With

            End If
            Return sukses
        End Function

        Public Function parsImg(ByRef imgList As List(Of String), ByVal input As String) As Boolean
            Dim sukses As Boolean = False
            imgList.Clear()
            If (String.IsNullOrEmpty(input) <> True) Then
                If (input.IndexOf(";") > 0) Then
                    'MessageBox.Show(input)
                    input = input.Replace(";", vbCrLf)
                    Dim x As New RichTextBox
                    x.Text = input

                    For Each line In x.Lines
                        'MessageBox.Show(line)
                        If String.IsNullOrEmpty(line) <> True Then
                            imgList.Add(line)
                        End If
                    Next
                Else
                    imgList.Add(input)
                End If
                sukses = True
            Else
                imgList.Clear()
            End If
            Return sukses
        End Function

    End Class
   
End Module
